<template>
  <div>
    身份证识别

    <div id="name"></div>
    <div id="idNumber"></div>
    <input type="file" id="fileInput" />
    <div id="camera"></div>
  </div>
</template>

<script>
// 引入完整功能
import { IDScanner } from "id-scanner-lib";
export default {
  data() {},
  async created() {
    const scanner = new IDScanner({
      onIDCardScanned: (info) => {
        console.log("识别到身份证信息:", info);
        document.getElementById("name").textContent = info.name;
        document.getElementById("idNumber").textContent = info.idNumber;
      },
    });
    await scanner.initialize();
    await scanner.startIDCardScanner(document.getElementById("camera"));
    // 使用文件输入处理
    document
      .getElementById("fileInput")
      .addEventListener("change", async (e) => {
        const file = e.target.files[0];
        try {
          // 先压缩图片提高处理速度
          const compressed = await scanner.compressImage(file, {
            maxSizeMB: 1,
            maxWidthOrHeight: 1920,
          });

          // 处理身份证图像
          const idInfo = await scanner.processIDCardImage(compressed);
          console.log("身份证信息:", idInfo);

          // 检查防伪检测结果
          if (idInfo.antiFakeResult) {
            console.log("防伪检测结果:", idInfo.antiFakeResult);
            if (idInfo.antiFakeResult.isAuthentic) {
              console.log("证件验证通过");
            } else {
              console.log("警告：可能为伪造证件");
            }
          }
        } catch (error) {
          console.error("处理失败:", error);
        }
      });
  },
};
</script>

<style scoped></style>
